<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/18 0018
 * Time: 9:17
 */

namespace app\jointly\controller;
use JPush\Client as JPush;
use think\Controller;
use think\Db;
use think\Request;

require_once VENDOR_PATH.'jpush-api-php-client-3.6.2/autoload.php';
class Push extends Controller
{
    private $key = 'd120057b22c2a379ecd40920';
    private $secret = 'b5b48f821258df9050f920b5';
    private $client;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->client = new JPush($this->key,$this->secret);
    }


    /*
     *  title 推送显示的标题
     *  content 推送显示内容
     */
    public function PushMsg($content,$title,$user){
        try{
            $this->client
                ->push()
                ->setPlatform('all')
                ->addRegistrationId($user)
                ->androidNotification($content,['title' => $title])
                ->iosNotification($content,['sound'=>'ring.aac'])
                ->setNotificationAlert($content)
              	->options(['apns_production' => true])
                ->send();
       }catch (\Exception $e){
           $set = [
               'time'   =>time(),
               'desc'   =>$title.'推送服务失败',
               'content'=>$e->getMessage(),
           ];
           Db::name('log')->insert($set);
       }
    }

    public function GetMessage($msgContent,$msgTitle,$data){
        $content = isset($data['content']) ? $data['content'] : false;
        $title   = isset($data['title'])   ? $data['title']   : false;
        $price   = isset($data['price'])   ? $data['price']   : false;
        $nowprice= isset($data['nowprice'])? $data['nowprice']: false;
        $buy_1   = isset($data['buy_1'])   ? $data['buy_1']   : false;
        $buy_2   = isset($data['buy_2'])   ? $data['buy_2']   : false;
        $buy_3   = isset($data['buy_3'])   ? $data['buy_3']   : false;
        $sell_1  = isset($data['sell_1'])  ? $data['sell_1']  : false;
        $sell_2  = isset($data['sell_2'])  ? $data['sell_2']  : false;
        $sell_3  = isset($data['sell_3'])  ? $data['sell_3']  : false;
        $platform= isset($data['platform'])? $data['platform']: false;
        $name    = isset($data['name'])    ? $data['name']    : false;      
      	$x = Db::name('price_set')->where(['id'=>2])->value('x');
      	$loss = 0;
        if($buy_1)$loss    = $buy_1/$x;
      	if($buy_2)$loss    = $buy_2/$x;
      	if($buy_3)$loss    = $buy_3/$x;
      	if($sell_1)$loss    = $sell_1/$x;
      	if($sell_2)$loss    = $sell_2/$x;
      	if($sell_3)$loss    = $sell_3/$x;
      	$loss = round($loss,2);	
        $times   = isset($data['time'])    ? $data['time']    : false;
        $type    = isset($data['type'])    ? $data['type']    : false;
        $num     = isset($data['num'])     ? $data['num']     : false;
        $time = date('Y-m-d H:i:s',time());
        $content = str_replace(['$content','$title','$price','$nowprice','$buy_1','$buy_2','$buy_3','$sell_1','$sell_2','$sell_3','$platform','$name','$loss','$time','$type','$num'],
                               [$content  , strtoupper($title) , $price , $nowprice , $buy_1 , $buy_2 , $buy_3 , $sell_1 , $sell_2 , $sell_3 , $platform , $name , $loss , $times, $type , $num],$msgContent);
        $title   = str_replace(['$title','$platform','$time'],[strtoupper($title),$platform,$times],$msgTitle);
        $set = [
            'title'  =>$title,
            'content'=>$content,
            'time'   =>$time,
        ];
        return $set;
    }
}